<template>
    <view class="uc-wrap uc-user-share">
        <view class="img">
            <image src="https://my.bziyun.com/20250619/user-share-img_1750324226371.png" mode="aspectFill"></image>
        </view>
        <view class="fixed-operate" @click="downLoad">
            <button class="uc-btn green" hover-class="none">保存名片海报后分享</button>
        </view>
    </view>
</template>
<script setup>
    import { ref } from 'vue'
	 const downLoad = async () => {
   	uni.getSetting({ 
   		success: function(res) {
   			if (res.authSetting['scope.writePhotosAlbum']) { // 用户授权相册权限
   				saveImg()
   			} else { // 用户没有授权相册权限
   				uni.showModal({
   					title: '提示',
   					content: '用户未授权相册权限，点击授权',
   					success: function (res) {
   						if (res.confirm) {
   							 uni.authorize({
   								scope: 'scope.writePhotosAlbum',
   								success: () => {
   									saveImg()
   								},
   								fail: () => { // 用户拒绝授权，引导手动开启
   								  uni.showModal({
   										title: '提示',
   										content: '需授权相册权限才能保存图片',
   										success: (res) => {
   										  if (res.confirm) uni.openSetting();
   										}
   								  });
   							  }	,
   							})
   						} else {
   							uni.showToast({
   							  title: "用户取消授权，不能使用下载功能",
   							  icon: "error",
   							})
   						}
   					},
   				})
   			}
   		},
   		fail: function(error) {
   			uni.showToast({
   			  title: "查询授权失败",
   			  icon: "error",
   			})
   		}
   	})
	}
   // 保存图片 -- 直接保存，不做重命名处理
  const saveImg = () => {
   	  // 先下载图片到本地
   	  uni.downloadFile({
   	    url: 'https://my.bziyun.com/20250622/695ce81350ebf5ae2a7d3868214a20766bc93c18569f7c1b36785fd76a0d3a9d_1750574482173.jpg', // 图片地址
   	    success: (res) => {
   	      if (res.statusCode === 200) {
   	        let tempFilePath = res.tempFilePath; // 获取下载后的本地路径
   	        // 下载成功后保存到相册
   	        uni.saveImageToPhotosAlbum({
   	          filePath: tempFilePath,
   	          success: (res) => {
   	            uni.showToast({
   	              title: "保存成功",
   	              icon: "success",
   	            });
   	          },
   	          fail: (err) => {
   	            console.log(err);
   	            uni.showToast({
   	              title: "保存失败",
   	              icon: "error",
   	            });
   	          },
   	        });
   	      } else {
   	        uni.showToast({
   	          title: "下载失败",
   	          icon: "error",
   	        });
   	      }
   	    },
   	    fail: (err) => {
   	      console.log('下载失败', err);
   	      uni.showToast({
   	        title: "下载失败",
   	        icon: "error",
   	      });
   	    },
   	  });
   	};
</script>